package de.koller.worlders.engine.tiles.landscape.generator.universe;

import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.worlders.engine.tiles.landscape.generator.IGenType;
import java.awt.image.BufferedImage;
import java.util.Random;

public class UniverseGenerator implements IGenType {

	private final static Random rnd = new Random();
	private final int universeWidth;
	private final int universeHeight;
	private final Region[] regions;
	private int count = 0;

	public UniverseGenerator( int universeWidth, int universeHeight, int regionNumber ) {
		this.universeWidth = universeWidth;
		this.universeHeight = universeHeight;
		this.regions = new Region[ regionNumber ];
		
		for( int i = 0; i < regions.length; i++ )
			regions[i] = new Region();
	}
	
	@Override
	public BufferedImage generateLight() {
		return null;
	}

	@Override
	public BufferedImage generateMap() {
		BufferedImage map = new BufferedImage( universeWidth, universeHeight, BufferedImage.TYPE_INT_ARGB );
		
		UniverseUtil.clear( map, 0xffffffff );
		new Cave().addRoot( map );
		//new PlanetShape().generateEllipse( map, 10, 10 );
		
		return map;
	}
	
	private void drawAt( int x, int y, int c, BufferedImage b ) {
		try {
			b.setRGB( x, y, c );
		} catch( Exception ex ) {}
	}
	
	private Region nearestRegion( float x, float y ) {
		float weight = regions[0].weight( x, y );
		Region region = regions[0];
		
		for( Region r : regions ) {
			float w = r.weight( x, y );
			if( w < weight ) {
				weight = w;
				region = r;
			}
		}
		
		return region;
	}
	
	private class Region {
		private final float x, y;
		private final int color;
		private int count;

		private Region() {
			this.x = rnd.nextFloat() * universeWidth;
			this.y = rnd.nextFloat() * universeHeight;
			this.color = ColorIntUtil.getIntColor( 0xff, rnd.nextInt( 0xff ), rnd.nextInt( 0xff ), rnd.nextInt( 0xff ) );
		}
		
		private float weight( float x, float y ) {
			return ((x - this.x)*(x - this.x) + (y - this.y)*(y - this.y));
		}
		
	}
	
	public static void main( String[] args ) {
		while( true )
			ImagePreview.showImage( new UniverseGenerator( 1200, 700, 4 ).generateMap(), 1200, 700 );
	}
}
